package com.leo.lime.music.view;

import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.TextView;

import android.content.Context;

import com.leo.lime.music.R;
import com.leo.lime.music.common.Logger;

/**
 * Created by android on 18-8-13.
 */

public class DrawableTextView extends TextView {
    private static final String TAG = "MainDrawerSearchTextView";

    private static final int LEFT = 0;
    private static final int TOP = 1;
    private static final int RIGHT = 2;
    private static final int BOTTOM = 3;

    private int mDrawableWidth;
    private int mDrawableHeight;
    private int mDrawableLocation;
    private int mDivider = -1;
    private Bitmap mDrawable;

    public DrawableTextView(Context ctx) {
        this(ctx, null);
    }

    public DrawableTextView(Context ctx, AttributeSet attrs) {
        this(ctx, attrs, 0);
    }

    public DrawableTextView(Context ctx, AttributeSet attrs, int defStyleAttr) {
        super(ctx, attrs, defStyleAttr);
        TypedArray ta = ctx.getTheme().obtainStyledAttributes(attrs, R.styleable.DrawableTextView, defStyleAttr, 0);
        int count = ta.getIndexCount();
        for (int i = 0; i < count; i++){
            int attr = ta.getIndex(i);
            switch (attr) {
                case R.styleable.DrawableTextView_drawable:
                    mDrawable = BitmapFactory.decodeResource(ctx.getResources(), ta.getResourceId(attr, 0));
                    break;
                case R.styleable.DrawableTextView_drawableWidth:
                    mDrawableWidth = ta.getDimensionPixelSize(attr, -1);
                    Logger.d(TAG, "mDrawableWidth = " + mDrawableWidth);
                    break;
                case R.styleable.DrawableTextView_drawableHeight:
                    mDrawableHeight = ta.getDimensionPixelSize(attr, -1);
                    Logger.d(TAG, "mDrawableHeight = " + mDrawableHeight);
                    break;
                case R.styleable.DrawableTextView_drawableLocation:
                    mDrawableLocation = ta.getInt(attr, LEFT);
                    break;
                //case R.styleable.DrawableTextView_divider:
                //    mDivider = ta.getDimensionPixelSize(attr, -1);
                //    break;
            }
        }
        ta.recycle();
        drawPicture();
    }

    public void setDrawable(Bitmap bitmap) {
        mDrawable = bitmap;
        drawPicture();
    }

    public void setDrawable(int resId) {
        mDrawable = BitmapFactory.decodeResource(getResources(), resId);
        drawPicture();
    }

    private void drawPicture() {
        if (mDrawable != null) {
            Drawable drawable;
            Logger.d(TAG, "mDrawableWidth = " + mDrawableWidth);
            Logger.d(TAG, "mDrawableHeight = " + mDrawableHeight);
            if (mDrawableHeight != -1 && mDrawableWidth != -1) {
                drawable = new BitmapDrawable(getResources(), getRealBitmap(mDrawable));
            } else {
                drawable = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(mDrawable, mDrawable.getWidth(), mDrawable.getHeight(), true));
            }
            if(mDivider != -1) {

            }
            switch (mDrawableLocation) {
                case LEFT:
                    this.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
                    break;
                case TOP:
                    this.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
                    break;
                case RIGHT:
                    this.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
                    break;
                case BOTTOM:
                    this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
                    break;
            }

        }
    }

    private Bitmap getRealBitmap(Bitmap image) {
        //根据需要Drawable原来的大小和目标宽高进行裁剪（缩放）
        int width = image.getWidth();// 获得图片的宽高
        int height = image.getHeight();
        // 取得想要缩放的matrix参数
        float scaleWidth = (float) mDrawableWidth / width;
        float scaleHeight = (float) mDrawableHeight / height;

        Logger.d(TAG, "mDrawableWidth = " + mDrawableWidth);
        Logger.d(TAG, "mDrawableHeight = " + mDrawableHeight);
        Logger.d(TAG, "width = " + width);
        Logger.d(TAG, "height = " + height);
        Logger.d(TAG, "scaleWidth = " + scaleWidth);
        Logger.d(TAG, "scaleHeight = " + scaleHeight);

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 返回新的Bitmap
        return Bitmap.createBitmap(image, 0, 0, width, height, matrix, true);
    }
}
